package com.zhongjian.technologycal.mall.activity.personal.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.activity.CourseDetailsActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.NumberPresenterActivity;
import com.zhongjian.technologycal.mall.adapter.FansListAdaper;
import com.zhongjian.technologycal.mall.adapter.PresenterNoAdapter;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.bean.FansBean;
import com.zhongjian.technologycal.mall.bean.PresenterNoBean;
import com.zhongjian.technologycal.mall.interfacemall.ListItemClick;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/15.
 */

public class PresenterNoFragment extends BaseFragment {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";

    @BindView(R.id.presenter_listView)
    ListView presenter_listView;
    private PresenterNoAdapter presenterNoAdapter;
    private List<PresenterNoBean> listItems = new ArrayList<>();

    @Override
    protected void initView() {
        initViewRequest();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_presenter_list;
    }

    public static PresenterNoFragment newInstance(String mCategoryName) {
        PresenterNoFragment categoryFragment = new PresenterNoFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_NAME, mCategoryName);
        categoryFragment.setArguments(bundle);
        return categoryFragment;
    }

    @Override
    public void initPresenter() {
        presenterNoAdapter = new PresenterNoAdapter(getActivity(), listItems, new ListItemClick() {
            @Override
            public void onClick(View item, View widget, int position, int which) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), NumberPresenterActivity.class);
                intent.putExtra("passiveUserId", String.valueOf(listItems.get(position).getData().get(position).getId()));
                startActivity(intent);

            }
        });
        presenter_listView.setAdapter(presenterNoAdapter);

    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else {
            HttpRetrofitClient.getInstance(getActivity())
                    .getUrlRequestPresenter(UserUtils.getUserToken(),
                            1, new Subscriber<ResponseBody>() {
                                @Override
                                public void onStart() {
                                    super.onStart();
                                }

                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(ResponseBody responseBody) {
                                    try {
                                        Gson gson = new Gson();
                                        PresenterNoBean presenterNoBean = gson.fromJson(responseBody.string(), PresenterNoBean.class);
                                        if (presenterNoBean.getError_code() == 0) {
                                            List<PresenterNoBean> forList = new ArrayList<>();
                                            for (int i = 0; i < presenterNoBean.getData().size(); i++) {
                                                forList.add(presenterNoBean);
                                            }
                                            setData(forList);

                                        } else if (presenterNoBean.getError_code() == 3) {
                                            Intent intent = new Intent();
                                            intent.setAction("com.broadcast.info_out_line");
                                            getActivity().sendBroadcast(intent);
                                        } else {
                                            ToastUitl.showShort(presenterNoBean.getError_message());
                                        }

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            });

        }
    }

    private void setData(List<PresenterNoBean> foreshowList) {
        for (PresenterNoBean vo : foreshowList) {
            listItems.add(vo);
        }
        presenterNoAdapter.notifyDataSetChanged();

    }
}
